var datatable;// 定义一个全局的dataTable
var attachChar;

//第二版变更结束**********
$(function() {
	
	$("#beginDate").val(getstartDate());
	$("#endDate").val(getendDate());
	$('#table tbody').on('click', 'tr', function() {
		if ($(this).hasClass('selected')) {
			$(this).removeClass('selected');
		} else {
			datatable.$('tr.selected').removeClass('selected');
			$(this).addClass('selected');
		}
	});
	
	//初始化图表
	psLineChar = echarts.init(document.getElementById('psLine'));
	
	getList();
})

function getList() {
	
	var startTime=$("#beginDate").val();  
    var endTime=$("#endDate").val();  
    if(!checkEndTime()){
		layer.alert("开始时间不能大于结束时间！", {
			icon : 3,
			offset: '100px'
		});
		return;
	};
	
	$("#tbodyId").remove();
	$('#table').append($("<tbody id='tbodyId'></tbody>"));
	if (datatable == undefined || datatable == "undefined" || datatable == null) {
	} else {
		datatable.destroy();
	}
	datatable = $('#table')
			.DataTable(
					{
						"aoColumnDefs" : [ {
							sDefaultContent : '',
							aTargets : [ '_all' ]
						} ],
						"bPaginate" : true, // 翻页功能
						"iDisplayLength" : 10,
						"bLengthChange" : false, // 改变每页显示数据数量
						"bFilter" : false, // 过滤功能
						"ordering": true, //排序功能
						"bSort" : true, // 排序功能
						"bInfo" : true,// 页脚信息
						"bAutoWidth" : true,// 自动宽度
						// "sPaginationType": "full_numbers",//显示分页的样式
						// 还有simple_numbers等
//						"sScrollX" : "100%",
						"sScrollXInner" : "110%",
						"bScrollCollapse" : true,
						"oLanguage" : {
							"sLengthMenu" : "每页显示 _MENU_条",
							"sZeroRecords" : "没有找到符合条件的数据",
							"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
							"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
							"sInfoEmpty" : "没有记录",
							"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
							"sSearch" : "搜索：",
							"oPaginate" : {
								"sFirst" : "首页",
								"sPrevious" : "前一页",
								"sNext" : "后一页",
								"sLast" : "尾页"
							}
						},
						"bProcessing" : true,
						"bServerSide" : true,
						"sAjaxSource" : "../../edpi/contentCDN/getList",
						"fnServerData" : function(sSource, aoData, fnCallback) {
							$
									.ajax({
										"type" : "post",
										"contentType" : "application/x-www-form-urlencoded",
										"url" : sSource,
										"dataType" : "json",
										"data" : {
											"aoData" : JSON.stringify(aoData),
											"startTime" :startTime,
											"endTime" :endTime,
											"cdn" :$("#cdn").val(),
											"flowType" :$("#flowType").val()
										}, // 以json格式传递
										"success" : function(resp) {
											fnCallback(resp.pageDate);
											d = resp.allData;
										}
									});
						},
						"fnDrawCallback"    : function(){
					        　　this.api().column(0).nodes().each(function(cell, i) {
					        　　　　cell.innerHTML =  i + 1;
					        　　});
					        },
						columns : [
								{
									data : "xuhao"
								},
								{
									data : "s_province"
								},
								{
									data : "avg_flow"
								},
								{
									data : "max_flow"
								}]

					});
}

function showPic() {

	var tab = $("#con");
	var pic = $("#pic");

	if (tab.is(":hidden")) {
		tab.show();
		pic.hide();
	} else {
		tab.hide();
		pic.show();

		initAttachChar();
	}
}

function initAttachChar() {
	var startTime=$("#beginDate").val();  
    var endTime=$("#endDate").val();  
    if(!checkEndTime()){
		layer.alert("开始时间不能大于结束时间！", {
			icon : 3,
			offset: '100px'
		});
		return;
	};
	
	// 查询
	psLineChar.clear();
	psLineChar.showLoading({
		text : '正在努力的读取数据中...'
	});
	$.ajax({
		type : "POST",
		url : "../../edpi/contentCDN/getAttachChar?rand="
				+ Math.random(),
		dataType : "json",
		"data" : {
			"startTime" :startTime,
			"endTime" :endTime,
			"cdn" :$("#cdn").val(),
			"flowType" :$("#flowType").val()
		},
		success : function(data) {
			psLineChar.setOption(data, true);
			psLineChar.hideLoading();
		}
	});
}

function resetQueryForm() {
	$('#queryForm')[0].reset();

	$("#eNodeBID").attr("disabled",true);
	$("#tac").attr("disabled",true);
	$("#village").attr("disabled",true);
	
	$("#eNodeBID_value").attr("readonly",true);
	$("#tac_value").attr("readonly",true);
	$("#village_value").attr("readonly",true);
}

function checkEndTime(){  
    var startTime=$("#beginDate").val();  
    var start=new Date(startTime.replace("-", "/").replace("-", "/"));  
    var endTime=$("#endDate").val();  
    var end=new Date(endTime.replace("-", "/").replace("-", "/"));  
    if(end<start){  
        return false;  
    }  
    return true;  
}

function getendDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 +"00"+seperator2+"00";
    return currentdate;
}

function getstartDate() {
    var date = new Date();
    date = date.valueOf();  
    date = date -  12 * 60 * 60 * 1000; 
    date = new Date(date);
    
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + date.getHours() + seperator2 +"00"+seperator2+"00";
    return currentdate;
}